package io.qar.controller;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import io.qar.commons.log.annotation.LogOperation;
import io.qar.commons.tools.utils.DateUtils;
import io.qar.commons.tools.utils.Result;
import io.qar.commons.tools.validator.ValidatorUtils;
import io.qar.commons.tools.validator.group.DefaultGroup;
import io.qar.commons.tools.validator.group.UpdateGroup;
import io.qar.dto.BackCalculateParamDTO;
import io.qar.dto.ScheduleJobDTO;
import io.qar.feign.ScheduleJobFeignClient;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("backcalculate")
public class BackCalculateController {

    @Autowired
    private ScheduleJobFeignClient scheduleJobFeignClient;


    @PostMapping("execute")  // /sys/backcalculate/execute
    @ApiOperation("执行回算任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "测试参数", paramType = "query", required = true, dataType = "int")
    })
    public Result<String> execute(@RequestBody Map<String, Object> params) {
        String name = MapUtil.getStr(params, "name");
        // 执行计算逻辑

        String format = DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");

        System.out.println(StrUtil.format("{} {}", format, name));
        return new Result<String>().ok("ok");
    }

    @PostMapping("create/{taskId}")
    @ApiOperation("保存回算并执行任务")
    @LogOperation("保存回算并执行任务")
    public Result saveRun(@PathVariable("taskId") Integer taskId, @RequestBody BackCalculateParamDTO backCalculateParamDTO) {
//        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        ScheduleJobDTO scheduleJobDTO = new ScheduleJobDTO();
        Long id = Long.valueOf(String.valueOf(taskId));
        scheduleJobDTO.setId(id);
        scheduleJobDTO.setRemark("回算任务");
        scheduleJobDTO.setBeanName("BackCalculateTask");
        String params = JSONObject.toJSONString(backCalculateParamDTO);
        scheduleJobDTO.setParams(params);
        scheduleJobDTO.setCronExpression("0/3 * * * * ?");
        scheduleJobDTO.setIsServiceInner(1);
        scheduleJobFeignClient.createResume(scheduleJobDTO);
        return new Result();
    }

    @PutMapping("update/{taskId}")
    @ApiOperation("修改回算任务")
    @LogOperation("修改回算任务")
    public Result updateResume(@PathVariable("taskId") Integer taskId, @RequestBody BackCalculateParamDTO backCalculateParamDTO) {
        ScheduleJobDTO dto = new ScheduleJobDTO();
        Long id = Long.valueOf(String.valueOf(taskId));
        dto.setId(id);
        dto.setRemark("回算任务");
        dto.setBeanName("BackCalculateTask");
        String params = JSONObject.toJSONString(backCalculateParamDTO);
        dto.setParams(params);
        dto.setCronExpression("0/3 * * * * ?");
        Result<ScheduleJobDTO> info = scheduleJobFeignClient.info(id);
        ScheduleJobDTO data = info.getData();
        Integer status = data.getStatus();
        dto.setStatus(status);
        dto.setIsServiceInner(1); // 使用方式  0：客户端调用  1：服务内调用
        scheduleJobFeignClient.update(dto);
        return new Result();
    }

    //
    @DeleteMapping
    @ApiOperation("回算任务删除")
    @LogOperation("回算任务删除")
    public Result delete(@RequestBody Integer[] ids) {
        List<Long> collect = Arrays.stream(ids).map(item -> Long.valueOf(String.valueOf(item))).collect(Collectors.toList());
        scheduleJobFeignClient.delete(collect.toArray(new Long[collect.size()]));
        // 删除回算任务
        return new Result();
    }
}
